   
const excel = {}
   function   s2ab(s) {
                if(typeof ArrayBuffer !== 'undefined') {
                    var buf = new ArrayBuffer(s.length);
                    var view = new Uint8Array(buf);
                    for(var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
                    return buf;
                } else {
                    var buf = new Array(s.length);
                    for(var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
                    return buf;
                }
            }


excel.export = function(columns,values,filename,firstRow,colWidth,callback){
    let data = []
    if ((firstRow != '') && (firstRow != undefined)){
        data.push([firstRow])

    }
    data.push(columns.map(e=> e.label))
    let key = columns.map(e=> e.prop)
    values.forEach(value =>{
        let newData = []
        key.forEach(value2 =>{
            newData.push(value[value2])
        })
        data.push(newData)
    })
    let sheet = XLSX.utils.json_to_sheet(data,{skipHeader: 1})
    let newBook = XLSX.utils.book_new()
    if ((firstRow != '') && (firstRow != undefined)){
         const merge = [
          { s: { r: 0, c: 0 }, e: { r: 0, c: 50 } }
        ];
        sheet["!merges"] = merge;
    }
    if (colWidth){
        sheet['!cols'] = colWidth;
    }
    XLSX.utils.book_append_sheet(newBook,sheet,'sheet1')
                     const wopts = {
                        bookType: 'xlsx',
                        bookSST: false,
                        type: 'binary'
                    };
    saveAs(new Blob([s2ab(XLSX.write(newBook, wopts))], {
        type: "application/octet-stream"
    }), filename + ".xlsx");
}

function Workbook() {
    if(!(this instanceof Workbook)) return new Workbook();
    this.SheetNames = [];
    this.Sheets = {};
}

excel.exportRaw = function(raw,filename,merge,colWidth,rowWidth,callback){
    // let ws = {}
    // // raw = [['1','2'],['2','3']]
    // var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    //     for(var R = 0; R != raw.length; ++R) {
    //     for(var C = 0; C != raw[R].length; ++C) {
    //         if(range.s.r > R) range.s.r = R;
    //         if(range.s.c > C) range.s.c = C;
    //         if(range.e.r < R) range.e.r = R;
    //         if(range.e.c < C) range.e.c = C;
    //         var cell = {v: raw[R][C],
    //               s: { alignment: {textRotation: 90 },
    //                    font: {sz: 14, bold: true, color: '#123456' }
    //             }
    //         };

    //         if (cell.v == null) {
    //             continue;
    //         }

    //         var cell_ref = XLSX.utils.encode_cell({c:C,r:R});

    //         if(typeof cell.v === 'number') cell.t = 'n';
    //         else if(typeof cell.v === 'boolean') cell.t = 'b';
    //         else if(cell.v instanceof Date) {
    //             cell.t = 'n'; cell.z = XLSX.SSF._table[14];
    //             cell.v = datenum(cell.v);
    //         }
    //         else cell.t = 's';

    //         ws[cell_ref] = cell;
    //     }
    //     if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
    // }
    let sheet = XLSX.utils.json_to_sheet(raw,{skipHeader: 1})
    let newBook = XLSX.utils.book_new()
    
    if (merge){
        sheet["!merges"] = merge;
    }
    if (colWidth){
        sheet['!cols'] = colWidth;
    }
    if (rowWidth){
        sheet['!rows'] = rowWidth;
    }
    
    XLSX.utils.book_append_sheet(newBook,sheet,'sheet1')
                     const wopts = {
                        bookType: 'xlsx',
                        bookSST: true,
                        type: 'binary'
                    };
    // let wb = new Workbook()
    // wb.SheetNames.push('1')
    // wb.Sheets['1']  = ws

    // XLSX.utils.book_append_sheet(wb,ws,'sheet1')
    // console.log(wb,'sd')
    // let test = XLSX.write(wb, wopts)
    let output = XLSX.write(newBook,wopts)
    // console.log(test,'sds')
    saveAs(new Blob([s2ab(output)], {
        type: "application/octet-stream"
    }), filename + ".xlsx");
}

export default excel